<?php

namespace App\Admin\Controllers\Config;

use App\Admin\Repositories\Config\VersionUpdate;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class VersionUpdateController extends AdminController
{


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new VersionUpdate(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('ostype');
            $grid->column('version');
            $grid->column('forced');
            $grid->column('address');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id','desc');
            $grid->disableFilterButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new VersionUpdate(), function (Show $show) {
            $show->field('id');
            $show->field('ostype');
            $show->field('title');
            $show->field('version');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new VersionUpdate(), function (Form $form) {
            $form->display('id');
            $form->select('ostype');
            $form->text('title');
            $form->text('version');
            $form->text('download_url');
            $form->switch('forced');
            $form->textarea('content');
            $form->display('created_at');
            $form->display('updated_at');
            $form->hidden('package');
            $form->saving(function (Form $form){
                //字符串截取
                $form->package = substr(strrchr($form->input("download_url"),"."),1,strlen($form->input("download_url")));
            });
        });
    }
}
